﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace QLTSCD
{
    public partial class frmNCC : DevComponents.DotNetBar.Office2007Form
    {
        int MaThaoTac = 0;
        int dong = 0;
        public frmNCC()
        {
            InitializeComponent();
        }

        private void frmNCC_Load(object sender, EventArgs e)
        {
            grvNCC_Load();

        }

        private void grvNCC_Load()
        {
            QLTSCDDataContext db = new QLTSCDDataContext();
            var listNCC = from ncc in db.NhaCungCaps
                          select ncc;
            grvNCC.AutoGenerateColumns = false;
            grvNCC.DataSource = listNCC;

            //for (int i = 0; i < listNCC.Count(); i++)
            //{
            //    grvNCC.Rows[i].Cells["Column1"].Value = (i + 1).ToString();
            //}
        }

        private void grvNCC_CellEnter(object sender, DataGridViewCellEventArgs e)
        {
            dong = e.RowIndex;
            QLTSCDDataContext db = new QLTSCDDataContext();
            int ID = int.Parse(grvNCC.Rows[dong].Cells["Column6"].Value.ToString());
            var NCC = (from ncc in db.NhaCungCaps
                       where ncc.MaNhaCungCap == ID
                       select ncc).First();
            txtName.Text = NCC.Ten;
            txtSDT.Text = NCC.SoDienThoai;
            txtEmail.Text = NCC.Email;
            txtDiaChi.Text = NCC.DiaChi;

        }

        private void buttonX2_Click(object sender, EventArgs e)
        {
            if (dong > 0)
            {
                dong = dong - 1;
            }
            grvNCC.CurrentCell = grvNCC.Rows[dong].Cells[0];
        }

        private void buttonX1_Click(object sender, EventArgs e)
        {
            grvNCC.CurrentCell = grvNCC.Rows[0].Cells[0];
        }

        private void buttonX3_Click(object sender, EventArgs e)
        {
            if (dong < grvNCC.Rows.Count - 1)
            {
                dong = dong + 1;
            }
            grvNCC.CurrentCell = grvNCC.Rows[dong].Cells[0];
        }

        private void buttonX4_Click(object sender, EventArgs e)
        {
            grvNCC.CurrentCell = grvNCC.Rows[grvNCC.Rows.Count - 1].Cells[0];
        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            MaThaoTac = 2;
            txtDiaChi.Text = "";
            txtName.Text = "";
            txtSDT.Text = "";
            txtEmail.Text = "";
            txtDiaChi.Enabled = true;
            txtEmail.Enabled = true;
            txtName.Enabled = true;
            txtSDT.Enabled = true;
            txtName.Focus();



        }

        private void btnThaoTac_Click(object sender, EventArgs e)
        {
            QLTSCDDataContext db = new QLTSCDDataContext();
            bool flag = false;
            string message = "";
            string Name = txtName.Text;
            string Address = txtDiaChi.Text;
            string Email = txtEmail.Text;
            string Phone = txtSDT.Text;
            switch (MaThaoTac)
            {
                case 0:
                    MessageBox.Show("Bạn chưa chọn thao tác!");
                    break;
                case 1:

                    string name = txtName.Text;
                    var ListNCC = from nccs in db.NhaCungCaps
                                  where nccs.Ten.Contains(name)
                                  select nccs;
                    grvNCC.DataSource = ListNCC;
                    break;
                case 2:

                    NhaCungCap ncc = new NhaCungCap();
                    ncc.Ten = Name;
                    ncc.DiaChi = Address;
                    ncc.Email = Email;
                    ncc.SoDienThoai = Phone;
                    db.NhaCungCaps.InsertOnSubmit(ncc);
                    try
                    {
                        db.SubmitChanges();
                        flag = true;
                    }
                    catch (Exception ex)
                    {
                        flag = false;
                        message = ex.Message;
                    }
                    if (flag)
                    {
                        MessageBox.Show("Bạn đã thêm thành công!", "Thông báo!", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                    else
                    {
                        MessageBox.Show("Lỗi: " + message, "Thông báo!", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                    grvNCC_Load();
                    break;
                case 3:
                    int MaNCC = int.Parse(grvNCC.Rows[dong].Cells["Column6"].Value.ToString());
                    NhaCungCap nhaCungCap = db.NhaCungCaps.Single(nhacungcap => nhacungcap.MaNhaCungCap == MaNCC);
                    if (txtName.Text.Trim() == "")
                    {
                        MessageBox.Show("Cần nhập tên nhà cung cấp!");
                        txtName.Focus();
                    }
                    else
                    {
                        nhaCungCap.Ten = txtName.Text;
                        nhaCungCap.SoDienThoai = txtSDT.Text;
                        nhaCungCap.DiaChi = txtDiaChi.Text;
                        nhaCungCap.Email = txtEmail.Text;
                        try
                        {
                            db.SubmitChanges();
                            flag = true;
                        }
                        catch (Exception ex)
                        {
                            flag = false;
                            message = ex.Message;
                        }
                        if (flag)
                        {
                            MessageBox.Show("Bạn đã sửa thành công!", "Thông báo!", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        }
                        else
                        {
                            MessageBox.Show("Lỗi: " + message, "Thông báo!", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        }
                        grvNCC_Load();
                    }
                    break;
                case 4:
                    int MaNCC1 = int.Parse(grvNCC.Rows[dong].Cells["Column6"].Value.ToString());
                    NhaCungCap nhaCungCap1 = db.NhaCungCaps.Single(nhacungcap => nhacungcap.MaNhaCungCap == MaNCC1);

                    try
                    {
                        DialogResult result = MessageBox.Show("Bạn có chắc xóa hay không?", "Thông báo!", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
                        if (result == DialogResult.OK)
                        {
                            db.NhaCungCaps.DeleteOnSubmit(nhaCungCap1);
                            db.SubmitChanges();
                            MessageBox.Show("Bạn đã xóa thành công!");
                        }
                    }
                    catch
                    {

                    }
                    grvNCC_Load();
                    break;

                    txtDiaChi.Enabled = false;
                    txtEmail.Enabled = false;
                    txtName.Enabled = false;
                    txtSDT.Enabled = false;

            }
        }



        

        private void btnTim_Click(object sender, EventArgs e)
        {
            MaThaoTac = 1;
            //ResetText
            txtDiaChi.Text = "";
            txtEmail.Text = "";
            txtName.Text = "";
            txtSDT.Text = "";

            //Enable Control
            txtSDT.Enabled = true;
            txtName.Enabled = true;
            txtEmail.Enabled = true;
            txtDiaChi.Enabled = true;
        }

        private void btnSua_Click(object sender, EventArgs e)
        {
            MaThaoTac = 3;
            txtName.Enabled = true;
            txtEmail.Enabled = true;
            txtSDT.Enabled = true;
            txtDiaChi.Enabled = true;

        }

        private void btnXoa_Click_1(object sender, EventArgs e)
        {
            MaThaoTac = 4;
           
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            DialogResult result = MessageBox.Show("Bạn có chắc chắn muốn thoát?", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (result == DialogResult.Yes)
                this.Close();
        }

        
        


    }
}

